/*
 * Copyright 2008-2010 Daniel Cachapa <cachapa@gmail.com>
 * 
 * This program is distributed under the terms of the GNU General Public License Version 3
 * The license can be read in its entirety in the LICENSE.txt file accompanying this source code,
 * or at: http://www.gnu.org/copyleft/gpl.html
 * 
 * This file is part of Libra.
 *
 * WeightWatch is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * WeightWatch is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the WeightWatch source code. If not, see: http://www.gnu.org/licenses
 */

package net.cachapa.libra.util;

import net.cachapa.libra.R;
import android.content.Context;

public class Bmi {
	// These values are the minimum to reach each level
	public final static double STARVATION = 0;
	public final static double SEVERELY_UNDERWEIGHT = 16;
	public final static double UNDERWEIGHT = 17;
	public final static double NORMAL = 18.5;
	public final static double OVERWEIGHT =	25;
	public final static double OBESE_I = 30;
	public final static double OBESE_II = 35;
	public final static double OBESE_III = 40;
	
	public final static double IDEAL_BMI = 22;
	
	public static float calculateBMI(double weight, double height, boolean metricHeight, boolean metricWeight) {
		if (!metricHeight) {
			// Assumes height in inches
			height *= 0.0254;
		}
		
		if (!metricWeight) {
			weight *= 0.45359237;
		}
		
		return (float) (weight / (height * height));
	}
	
	public static float getWeight(double bmi, double height, boolean metricHeight, boolean metricWeight) {
		if (!metricHeight) {
			height *= 0.0254;
		}
		
		float weight = (float) (bmi * height * height);
		
		if (!metricWeight) {
			weight /= 0.45359237;
		}
		
		return weight;
	}
	
	public static String getLevelName(double bmi, Context context) {
		if (bmi < SEVERELY_UNDERWEIGHT)
			return context.getString(R.string.starvation);
		if (bmi < UNDERWEIGHT)
			return context.getString(R.string.severely_underweight);
		if (bmi < NORMAL)
			return context.getString(R.string.underweight);
		if (bmi < OVERWEIGHT)
			return context.getString(R.string.normal);
		if (bmi < OBESE_I)
			return context.getString(R.string.overweight);
		if (bmi < OBESE_II)
			return context.getString(R.string.obese_class_i);
		if (bmi < OBESE_III)
			return context.getString(R.string.obese_class_ii);
		return context.getString(R.string.obese_class_iii);
	}
	
	public static double getIdealWeight(double height, boolean metricHeight, boolean metricWeight) {
		return getWeight(IDEAL_BMI, height, metricHeight, metricWeight);
	}
}
